package com.ruoyi.kjcg.gfbzck.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.kjcg.gfbzck.domain.GfbzckFj;
import com.ruoyi.kjcg.gfbzck.mapper.GfbzckMapper;
import com.ruoyi.kjcg.gfbzck.domain.Gfbzck;
import com.ruoyi.kjcg.gfbzck.service.IGfbzckService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 工法编制参考Service业务层处理
 *
 * @author huacai
 * @date 2024-11-02
 */
@Service
public class GfbzckServiceImpl implements IGfbzckService {
    @Resource
    private GfbzckMapper gfbzckMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询工法编制参考
     *
     * @param gfbzckId 工法编制参考主键
     * @return 工法编制参考
     */
    @Override
    public Gfbzck selectGfbzckByGfbzckId(String gfbzckId) {
        return gfbzckMapper.selectGfbzckByGfbzckId(gfbzckId);
    }

    /**
     * 查询工法编制参考列表
     *
     * @param gfbzck 工法编制参考
     * @return 工法编制参考
     */
    @Override
    public List<Gfbzck> selectGfbzckList(Gfbzck gfbzck) {
        return gfbzckMapper.selectGfbzckList(gfbzck);
    }

    /**
     * 新增工法编制参考
     *
     * @param gfbzck 工法编制参考
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGfbzck(Gfbzck gfbzck) {
        gfbzck.setCreateTime(DateUtils.getNowDate());
        int rows = gfbzckMapper.insertGfbzck(gfbzck);
        insertGfbzckFj(gfbzck);
        return rows;
    }

    /**
     * 批量新增工法编制参考
     *
     * @param gfbzcks 工法编制参考List
     * @return 结果
     */
    @Override
    public int batchInsertGfbzck(List<Gfbzck> gfbzcks) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(gfbzcks)) {
            try {
                for (int i = 0; i < gfbzcks.size(); i++) {
                    int row = gfbzckMapper.insertGfbzck(gfbzcks.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == gfbzcks.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改工法编制参考
     *
     * @param gfbzck 工法编制参考
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGfbzck(Gfbzck gfbzck) {
        gfbzck.setUpdateTime(DateUtils.getNowDate());
//        gfbzckMapper.deleteGfbzckFjByGfbzckId(gfbzck.getGfbzckId());
        insertGfbzckFj(gfbzck);
        return gfbzckMapper.updateGfbzck(gfbzck);
    }

    /**
     * 批量删除工法编制参考
     *
     * @param gfbzckIds 需要删除的工法编制参考主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGfbzckByGfbzckIds(String[] gfbzckIds) {
        gfbzckMapper.deleteGfbzckFjByGfbzckIds(gfbzckIds);
        return gfbzckMapper.deleteGfbzckByGfbzckIds(gfbzckIds);
    }

    /**
     * 删除工法编制参考信息
     *
     * @param gfbzckId 工法编制参考主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGfbzckByGfbzckId(String gfbzckId) {
        gfbzckMapper.deleteGfbzckFjByGfbzckId(gfbzckId);
        return gfbzckMapper.deleteGfbzckByGfbzckId(gfbzckId);
    }

    /**
     * 新增附件
     *
     * @param gfbzckFj
     * @return
     */
    @Override
    public int insertGfBzCkFjBygfbzckId(GfbzckFj gfbzckFj) {
        return gfbzckMapper.insertGfBzCkFjBygfbzckId(gfbzckFj);
    }

    /**
     * 根据ID查询附件列表
     *
     * @param gfbzckFj
     * @return
     */
    @Override
    public List<GfbzckFj> selectGfBzCkFjList(GfbzckFj gfbzckFj) {
        return gfbzckMapper.selectGfBzCkFjList(gfbzckFj);
    }

    /**
     * 批量删除工法编制参考附件
     *
     * @param fjIds 需要删除的工法编制参考附件主键
     * @return 结果
     */
    @Override
    public int deleteGfbzckFjByFjIds(Integer[] fjIds) {
        return gfbzckMapper.deleteGfbzckFjByFjIds(fjIds);
    }

    /**
     * 新增工法编制参考附件信息
     *
     * @param gfbzck 工法编制参考对象
     */
    public void insertGfbzckFj(Gfbzck gfbzck) {
        List<GfbzckFj> gfbzckFjList = gfbzck.getGfbzckFjList();
        String gfbzckId = gfbzck.getGfbzckId();
        if (StringUtils.isNotNull(gfbzckFjList)) {
            List<GfbzckFj> list = new ArrayList<GfbzckFj>();
            for (GfbzckFj gfbzckFj : gfbzckFjList) {
                gfbzckFj.setGfbzckId(gfbzckId);
                list.add(gfbzckFj);
            }
//            if (list.size() > 0) {
//                gfbzckMapper.batchGfbzckFj(list);
//            }
        }
    }
}
